home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / source code / Dos / pcmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-17  |  7.2 KB  |  238 lines  |  [TEXT/R*ch]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4. Additional Portions Copyright 1992 by Mark Anacker
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its
  9. documentation for any purpose and without fee is hereby granted,
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in
  12. supporting documentation, and that the names of Stichting Mathematisch
  13. Centrum or CWI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior permission.
  15.  
  16. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  17. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  19. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  20. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  21. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  22. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  
  24. ******************************************************************/
  25.  
  26. /* PC module implementation
  27.  
  28.         This is my first crack at a low-level interface module for
  29.         IBM-PC compatibles.  It makes heavy use of the bios routines
  30.         that are available from Turbo C.
  31. */
  32.  
  33. #include <signal.h>
  34. #include <string.h>
  35.  
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <time.h>
  39. #include <dir.h>
  40. #include <io.h>
  41.  
  42. #ifdef MSDOS
  43. #include <dos.h>
  44. #include <fcntl.h>
  45. #include <bios.h>
  46. #include <mem.h>
  47.  
  48. struct timeval {
  49.         unsigned long tv_sec;
  50.         long    tv_usec;
  51.         };
  52.  
  53. #endif
  54.  
  55. #include "allobjec.h"
  56. #include "modsuppo.h"
  57.  
  58. typedef unsigned char byte;
  59. typedef unsigned int uint;
  60. typedef unsigned long ulong;
  61.  
  62. extern char *strerror PROTO((int));
  63.  
  64. /* PC methods */
  65.  
  66. /* bioscom - BIOS com port interface ----------------------------------------
  67.  
  68.         arguments:      cmd=0   set comm. parms. in char on port
  69.                         cmd=1   send char to port
  70.                         cmd=2   return char from port
  71.                         cmd=3   return comm port status
  72. -------------------------------------------------------------------------- */
  73. static object *
  74. pc_bioscom(self, args)          /* args = (cmd, port, char)     */
  75.         object *self;
  76.         object *args;
  77. {
  78. int cmd,port;
  79. byte *achar;
  80. register uint rv;
  81.  
  82.     if (!getargs(args, "(iii)", &cmd, &port, &achar))
  83.         return NULL;
  84.  
  85.         rv = (uint) bioscom(cmd,*achar,port);
  86.  
  87.     return mkvalue("(ii)", rv & 0xff, (rv & 0xff00) >> 8);
  88. }
  89.  
  90. /* biosdisk - BIOS disk services --------------------------------------------
  91.  
  92.         Interfaces to the low-level BIOS disk code.
  93. -------------------------------------------------------------------------- */
  94. static object *
  95. pc_biosdisk(self, args)         /* args = (cmd, drive, head, track, sector, */
  96.         object *self;           /*         num_sectors, buffer)             */
  97.         object *args;
  98. {
  99. int cmd, drive, head, track, sector, nsects, rv;
  100. void *buffer;
  101.  
  102.     /* XXX Does this work?  It used to have getpointarg with the buffer
  103.        XXX as only argument, which doesn't feel right either... */
  104.     if (!getargs(args, "(iiiiiil)",
  105.              &cmd, &drive, &head, &track, §or, &nsects, &buffer))
  106.                 return NULL;
  107.  
  108.         rv = biosdisk(cmd,drive,head,track,sector,nsects,buffer);
  109.         return(newintobject((long) rv));
  110. }
  111.  
  112. /* biosequip - BIOS equipment scan ------------------------------------------
  113.  
  114.         returns a tuple of 5 numbers giving the display type, number of
  115.         floppy drives, number of com ports, game ports, and printers.
  116. -------------------------------------------------------------------------- */
  117. static object *
  118. pc_biosequip(self)              /* no args */
  119.         object *self;
  120. {
  121. uint rv;
  122. struct eflag_st {
  123.   unsigned int res1 : 4;
  124.   unsigned int disp : 2;        /* display type */
  125.   unsigned int flop : 2;        /* number of floppies */
  126.   unsigned int res2 : 1;
  127.   unsigned int ports : 3;       /* number of COM ports */
  128.   unsigned int game : 1;        /* number of game ports */
  129.   unsigned int res3 : 1;
  130.   unsigned int print : 2;       /* number of printers */
  131. } eflags;
  132.  
  133.         rv = (uint) biosequip();
  134.         memcpy(&eflags,&rv,sizeof(rv));
  135.     return mkvalue("(iiiii)", eflags.disp, eflags.flop+1,
  136.                eflags.ports, eflags.game, eflags.print);
  137.         return v;
  138. }
  139.  
  140. /* biosmemory - BIOS memory size --------------------------------------------
  141.  
  142.         returns the number of Kbytes of low RAM
  143. -------------------------------------------------------------------------- */
  144. static object *
  145. pc_biosmemory(self)             /* no args */
  146.         object *self;
  147. {
  148. register uint rv;
  149.  
  150.         rv = (uint) biosmemory();
  151.         return newintobject((long) rv);
  152. }
  153.  
  154. /* biostime - BIOS timer count ----------------------------------------------
  155.  
  156.         arguments:      cmd=0   return ticks since midnight
  157.                         cmd=1   set tick counter
  158. */
  159. static object *
  160. pc_biostime(self, args)
  161.         object *self;
  162.         object *args;                   /* args = (cmd, ticks) */
  163. {
  164. ulong   bcmd,btime;
  165. object  *v;
  166.  
  167.     if (!getargs(args, "(ll)", &bcmd, &btime))
  168.         return NULL;
  169.  
  170.         btime = (ulong) biostime(bcmd,btime);
  171.         return newlongobject((long) btime);
  172. }
  173.  
  174. /* biosprint - BIOS printer interface ---------------------------------------
  175.  
  176.         arguments:      cmd=0   send char to port
  177.                         cmd=1   initialize port
  178.                         cmd=2   return port status
  179. -------------------------------------------------------------------------- */
  180. static object *
  181. pc_biosprint(self, args)                /* args = (cmd, port, char)     */
  182.         object *self;
  183.         object *args;
  184. {
  185. int cmd,port;
  186. char achar[2];
  187. register uint rv;
  188.  
  189.     if (!getargs(args, "(iii)", &cmd, &port, &achar))
  190.         return NULL;
  191.  
  192.         rv = (uint) biosprint(cmd,achar[0],port);
  193.         return newintobject((long) rv);
  194. }
  195.  
  196. /* bioskey - BIOS keyboard interface ----------------------------------------
  197.  
  198.         arguments:      cmd=0   return scan code, leave in buffer
  199.                                 (wait for key if buffer is empty)
  200.                         cmd=1   return scan code, remove from buffer
  201.                                 (return 0 if buffer is empty)
  202.                         cmd=2   return shift-state flags
  203. -------------------------------------------------------------------------- */
  204. static object *
  205. pc_bioskey(self, args)          /* args = cmd   */
  206.         object *self;
  207.         object *args;
  208. {
  209. int cmd;
  210. register uint rv;
  211.  
  212.         if (!getargs(args, "i", &cmd))
  213.                 return NULL;
  214.         rv = (uint) bioskey(cmd);
  215.     return mkvalue("(ii)", rv & 0xff, (rv & 0xff00) >> 8);
  216. }
  217.  
  218. static struct methodlist pc_methods[] = {
  219.         {"bioscom",     pc_bioscom},
  220.         {"biosdisk",    pc_biosdisk},
  221.         {"biosequip",   pc_biosequip},
  222.         {"biosmemory",  pc_biosmemory},
  223.         {"biostime",    pc_biostime},
  224.         {"biosprint",   pc_biosprint},
  225.         {"bioskey",     pc_bioskey},
  226.         {NULL,          NULL}            /* Sentinel */
  227. };
  228.  
  229.  
  230. void
  231. initpc()
  232. {
  233.         object *m, *d, *v;
  234.  
  235.         m = initmodule("pc", pc_methods);
  236.         d = getmoduledict(m);
  237. }
  238.